#include "Material.h"

int Material::read(TiXmlElement* material)
{
	float shininess_value;
	float temp1,temp2,temp3,temp4;
	for(TiXmlElement *res(material->FirstChildElement()); res != NULL; res = res->NextSiblingElement())
	{
		string tag_view=res->Value();
		if(tag_view=="emission")
		{
			if (res->QueryFloatAttribute("r",&temp1)==TIXML_SUCCESS&&
			   res->QueryFloatAttribute("g",&temp2)==TIXML_SUCCESS&&
			   res->QueryFloatAttribute("b",&temp3)==TIXML_SUCCESS&&
			   res->QueryFloatAttribute("a",&temp4)==TIXML_SUCCESS)
			{
				emission[0]=temp1;
				emission[1]=temp2;
				emission[2]=temp3;
				emission[3]=temp4;
			}
			else
			{
				cout<<"erro na leitura de emission"<<endl;
				return -1;
			}
		}
		else if(tag_view=="ambient")
		{
			if (res->QueryFloatAttribute("r",&temp1)==TIXML_SUCCESS&&
			   res->QueryFloatAttribute("g",&temp2)==TIXML_SUCCESS&&
			   res->QueryFloatAttribute("b",&temp3)==TIXML_SUCCESS&&
			   res->QueryFloatAttribute("a",&temp4)==TIXML_SUCCESS)
			{
				ambient[0]=temp1;
				ambient[1]=temp2;
				ambient[2]=temp3;
				ambient[3]=temp4;
			}
			else
			{
				cout<<"erro na leitura de ambient"<<endl;
				return -1;
			}
		}
		else if(tag_view=="diffuse")
		{
			if (res->QueryFloatAttribute("r",&temp1)==TIXML_SUCCESS&&
			   res->QueryFloatAttribute("g",&temp2)==TIXML_SUCCESS&&
			   res->QueryFloatAttribute("b",&temp3)==TIXML_SUCCESS&&
			   res->QueryFloatAttribute("a",&temp4)==TIXML_SUCCESS)
			{
				diffuse[0]=temp1;
				diffuse[1]=temp2;
				diffuse[2]=temp3;
				diffuse[3]=temp4;
			}
			else
			{
				cout<<"erro na leitura de diffuse"<<endl;
				return -1;
			}
		}
		else if(tag_view=="specular")
		{
			if (res->QueryFloatAttribute("r",&temp1)==TIXML_SUCCESS&&
			   res->QueryFloatAttribute("g",&temp2)==TIXML_SUCCESS&&
			   res->QueryFloatAttribute("b",&temp3)==TIXML_SUCCESS&&
			   res->QueryFloatAttribute("a",&temp4)==TIXML_SUCCESS)
			{
				specular[0]=temp1;
				specular[1]=temp2;
				specular[2]=temp3;
				specular[3]=temp4;
			}
			else
			{
				cout<<"erro na leitura de specular"<<endl;
				return -1;
			}
		}
		else if(tag_view=="shininess")
		{
			if (res->QueryFloatAttribute("value",&temp1)==TIXML_SUCCESS)
			{
				shininess[0]=temp1;
			}
			else
			{
				cout<<"erro na leitura de shininess"<<endl;
				return -1;
			}
		}
		
		//if(ashininess->QueryFloatAttribute("value",&shininess_value)==TIXML_SUCCESS)
	}
	
	return 1;
}